package com.bookmanger.server.task;

import com.bookmanger.server.service.FineService;
import com.bookmanger.server.service.PreBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class OverdueCheckTask {
    
    @Autowired
    private FineService fineService;
    
    @Autowired
    private PreBookService preBookService;
    
    /**
     * 每天凌晨1点执行逾期检查
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void checkOverdueBooks() {
        log.info("开始执行逾期图书检查任务");
        try {
            fineService.checkOverdueBooksAndSendNotifications();
            log.info("逾期图书检查任务执行完成");
        } catch (Exception e) {
            log.error("执行逾期图书检查任务时发生错误", e);
        }
    }
    
    /**
     * 每天晚上10点执行预定超时检查
     */
    @Scheduled(cron = "0 0 22 * * ?")
    public void checkOverduePreBooks() {
        log.info("开始执行预定超时检查任务");
        try {
            preBookService.handleOverduePreBooks();
            log.info("预定超时检查任务执行完成");
        } catch (Exception e) {
            log.error("执行预定超时检查任务时发生错误", e);
        }
    }
}